package com.winai.launcher.view.set;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.manager.AysDeviceManager;
import com.winai.launcher.override.ToastKs;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.heaton.blelibrary.ble.callback.BleNotiftCallback;
import cn.com.heaton.blelibrary.ble.callback.BleWriteCallback;
import cn.com.heaton.blelibrary.ble.model.BleDevice;
import cn.com.heaton.blelibrary.ble.utils.ByteUtils;
import cn.com.heaton.blelibrary.ble.utils.ThreadUtils;

public class DeviceControlActivity extends BaseActivity {

    private static final String TAG = "DeviceControlActivity";
    public static final String EXTRA_TAG = "device";
    @BindView(R.id.str_title)
    TextView strTitle;
    @BindView(R.id.tv_data)
    TextView tvData;
    private BleDevice bleDevice;
    private BluetoothGattService service;
    private Context mContext = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_control);
        ButterKnife.bind(this);
        start();
    }

    private void start() {
        strTitle.setText("双头温阳仪");
        initObject();
        openNotify();
    }

    private void initObject() {
        bleDevice = getIntent().getParcelableExtra(EXTRA_TAG);
        if (bleDevice == null) finish();
        service = AysDeviceManager.getInstance(mContext)
                .getBluetoothGattService(bleDevice.getBleAddress(), AysDeviceManager.getInstance(mContext).uuid);
        if (service == null) finish();
    }

    private void openNotify() {
        if (service == null) {
            ThreadUtils.ui(new Runnable() {
                @Override
                public void run() {
                    ToastKs.show(DeviceControlActivity.this, "未连接此设备");
                }
            });
            finish();
            return;
        }
        AysDeviceManager.getInstance(mContext).openNotify(bleDevice.getBleAddress(), new BleNotiftCallback<BleDevice>() {
            @Override
            public void onChanged(BleDevice device, final BluetoothGattCharacteristic characteristic) {
                ThreadUtils.ui(new Runnable() {
                    @Override
                    public void run() {
                        tvData.setText(String.format("value: %s%s", "(0x)", ByteUtils.bytes2HexStr(characteristic.getValue())));
                    }
                });
            }

            @Override
            public void onNotifySuccess(BleDevice device) {
                super.onNotifySuccess(device);
            }

            @Override
            public void onNotifyCanceled(BleDevice device) {
                super.onNotifyCanceled(device);
            }
        });
    }

    private void sendWrite(String status) {
        String command = "";
        switch (status) {
            case "open":
                command = "0a010101";
                break;
            case "close":
                command = "0a010100";
                break;
        }
        AysDeviceManager.getInstance(mContext).sendWrite(bleDevice.getBleAddress(), command
                , new BleWriteCallback<BleDevice>() {
                    @Override
                    public void onWriteSuccess(BleDevice device, BluetoothGattCharacteristic characteristic) {
                        ThreadUtils.ui(new Runnable() {
                            @Override
                            public void run() {
                                ToastKs.show(mContext, "操作成功");
                            }
                        });
                    }

                    @Override
                    public void onWiteFailed(BleDevice device, int failedCode) {
                        super.onWiteFailed(device, failedCode);
                        ThreadUtils.ui(new Runnable() {
                            @Override
                            public void run() {
                                ToastKs.show(mContext, "操作失败:" + failedCode);
                            }
                        });
                    }
                });
    }

    @OnClick({R.id.iv_back, R.id.iv_home, R.id.ll_open, R.id.ll_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_home:
                //模拟HOME
                Intent it = new Intent(Intent.ACTION_MAIN);
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                it.addCategory(Intent.CATEGORY_HOME);
                startActivity(it);
                break;
            case R.id.ll_open:
                sendWrite("open");
                break;
            case R.id.ll_close:
                sendWrite("close");
                break;
        }
    }
}